package com.changan.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.changan.condition.UserRegisterCondition;
import com.changan.entity.User;
import com.changan.exception.BusinessException;
import com.changan.response.Result;
import com.changan.response.ResultCode;
import com.changan.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


@Api
@CrossOrigin
@RestController
@RequestMapping(value = "/api")
public class UserController {
    @Autowired
    private UserService userService;


    /**
     * 注册之前要确定用户是否已经存在
     */
    @GetMapping("/ckusr")
    public Result checkUser(String username) {
        if (StringUtils.isEmpty(username)) {
            return Result.error(ResultCode.PARAM_IS_BLANK);
        }
        //判断用户是否存在
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUserName, username);
        User user = userService.getOne(queryWrapper);
        if (user != null) {
            return Result.error(ResultCode.USER_ACCOUNT_ALREADY_EXIST);
        }
        return Result.ok();
    }

    @ApiOperation(value = "用户注册", notes = "传入用户的注册信息")
    @PostMapping("/registry")
    public Result registry(@RequestBody UserRegisterCondition condition) {
        boolean b = userService.userRegister(condition);
        return Result.ok();
    }
}
